﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;


namespace UncleBobWebService.Database
{
    class ApplicantDBAdapter : IDBAdapter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="applicant"></param>
        /// <returns></returns>
        public override Message CreateData(Message msg, Object applicant)
        {
            mCommand = mConnection.CreateCommand();

            mCommand.CommandText = string.Format("INSERT INTO UserRegistration (UserName, Password, FirstName, MI, LastName, Address, PhoneNumber) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')"
                    , ((RegisterModel)applicant).Username
                    , ((RegisterModel)applicant).Password
                    , ((RegisterModel)applicant).FirstName
                    , ((RegisterModel)applicant).MI
                    , ((RegisterModel)applicant).LastName
                    , ((RegisterModel)applicant).Address
                    , ((RegisterModel)applicant).PhoneNumber
            );

            try
            {
                mConnection.Open();
                mCommand.ExecuteNonQuery();
                mConnection.Close();
                msg.Result = true;
            }
            catch (Exception error)
            {
                msg.Result = false;
                msg.Data = new String(error.Message.ToCharArray());
            }

            return msg;
        }


       public override Message SearchForSpecificData(Message msg, Object user)
        {
            mTable = new DataTable();
            mCommand = mConnection.CreateCommand();
            mCommand.CommandText = string.Format("SELECT * FROM UserRegistration WHERE UserName = '{0}' AND Password = '{1}'"
                    , ((RegisterModel)user).Username
                    , ((RegisterModel)user).Password
            );
            SqlDataAdapter mAdapter = new SqlDataAdapter(mCommand);

            try
            {
                mConnection.Open();
                mAdapter.Fill(mTable);
                mConnection.Close();

                if (mTable.Rows.Count != 0)
                {
                    msg.Result = true;
                    ((RegisterModel)msg.Data).Username = mTable.Rows[0]["UserName"].ToString();
                    ((RegisterModel)msg.Data).FirstName = mTable.Rows[0]["FirstName"].ToString();
                    ((RegisterModel)msg.Data).LastName = mTable.Rows[0]["LastName"].ToString();
                }
                else
                {
                    msg.Result = false;
                    msg.Data = new String(string.Format("Account does not exist.\nUsername/Password miss match").ToCharArray());
                }
            }
            catch (Exception error)
            {
                msg.Result = false;
                msg.Data = new String(error.Message.ToCharArray());
            }

            return msg;
        }


        public override Message SearchForAllData(Message msg)
        {
            throw new NotImplementedException();
        }


        public override Message EditSpecificData(Message msg, Object data)
        {
            throw new NotImplementedException();
        }


        public override Message DeleteSpecificData(Message msg, Object data)
        {
            throw new NotImplementedException();
        }


        public override Message DeleteAllData(Message msg)
        {
            throw new NotImplementedException();
        }
    }
}
